// Copyright (c) 2012, Geeekzor
// 
// All rights reserved.
// 
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met: 
// 
// 1. Redistributions of source code must retain the above copyright notice, this
//    list of conditions and the following disclaimer. 
// 2. Redistributions in binary form must reproduce the above copyright notice,
//    this list of conditions and the following disclaimer in the documentation
//    and/or other materials provided with the distribution. 
// 
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 
// The views and conclusions contained in the software and documentation are those
// of the authors and should not be interpreted as representing official policies, 
// either expressed or implied, of the GeekyIrc Project.
// 
// Created 2012 05 29 16:15
// Updated 2012 05 30 21:34


namespace GeekyIrc
{
    using System;
    using System.Drawing;
    using System.Globalization;
    using System.IO;
    using Meebey.SmartIrc4net;
    using Styx;
    using Styx.Helpers;

    public partial class GeekyIrc
    {
        /// <summary>
        ///   Geeky Log, prints to Honorbuddys Log Window
        /// </summary>
        /// <param name="format"> Message </param>
        /// <param name="pargs"> Optional Arguments </param>
        public static void Write(string format, params object[] pargs)
        {
            Logging.Write(Color.Yellow, string.Format("[GeekyIrc] {0}", String.Format(format, pargs)));
        }

        /// <summary>
        ///   Geeky Log, prints to Honorbuddys Log Window
        /// </summary>
        /// <param name="color"> Color </param>
        /// <param name="format"> Message </param>
        /// <param name="pargs"> Optional Arguments </param>
        public static void Write(Color color, string format, params object[] pargs)
        {
            Logging.Write(color, string.Format("[GeekyIrc] {0}", String.Format(format, pargs)));
        }

        /// <summary>
        ///   Geeky Debug, prints to Honorbuddys Debug Window
        /// </summary>
        /// <param name="format"> Message </param>
        /// <param name="args"> Optinal Arguments </param>
        public static void Debug(string format, params object[] args)
        {
            Debug(Color.Yellow, format, args);
        }

        /// <summary>
        ///   Geeky Debug, prints to Honorbuddys Debug Window
        /// </summary>
        /// <param name="color"> Color </param>
        /// <param name="format"> Message </param>
        /// <param name="args"> Optional arguments </param>
        public static void Debug(Color color, string format, params object[] args)
        {
            if (ConfigValues.Instance.DebugLogging)
            {
                Logging.Write
                    (color,
                     "[{0}] {1}",
                     DateTime.Now.ToShortTimeString().ToString(CultureInfo.InvariantCulture),
                     String.Format(format, args));
            }
            else
            {
                Logging.WriteDebug
                    (color,
                     "[{0}] {1}",
                     DateTime.Now.ToShortTimeString().ToString(CultureInfo.InvariantCulture),
                     String.Format(format, args));
            }

            WriteDebugLog(String.Format("[{0}] [DEBUG] {1}", DateTime.Now, String.Format(format, args)));
        }

        /// <summary>
        ///   Write "Write" to file
        /// </summary>
        /// <param name="msg"> Message </param>
        public static void WriteLog(object msg)
        {
            string path = Logging.ApplicationPath +
                          String.Format(@"\Plugins\GeekyIrc\Logs\{0}-IrcLog.log", StyxWoW.Me.Name);


            try
            {
                string dir = Logging.ApplicationPath + @"\Plugins\GeekyIrc\Logs\";
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);

                StreamWriter sw = File.AppendText(path);

                string logLine = String.Format("{0:G}: {1}.", DateTime.Now, msg);
                sw.WriteLine(logLine);
                sw.Close();
            }
            catch (Exception file)
            {
                Debug(String.Format("[LOGFILE] {0}", file.Message));
            }
        }

        /// <summary>
        ///   Write Debug Messages to file
        /// </summary>
        /// <param name="msg"> Message </param>
        public static void WriteDebugLog(object msg)
        {
            string path = Logging.ApplicationPath +
                          String.Format(@"\Plugins\GeekyIrc\Logs\{0}-IrcDebug.log", StyxWoW.Me.Name);


            try
            {
                string dir = Logging.ApplicationPath + @"\Plugins\GeekyIrc\Logs\";
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);

                StreamWriter sw = File.AppendText(path);
                string logLine = String.Format("{0}: {1}.", DateTime.Now, msg);
                sw.WriteLine(logLine);
                sw.Close();
            }
            catch (Exception file)
            {
                Logging.WriteDebug(String.Format("[LOGFILE] {0}", file.Message));
            }
        }

        /// <summary>
        ///   Write Chat to file
        /// </summary>
        /// <param name="msg"> Message </param>
        public static void WriteChatLog(object msg)
        {
            string path = Logging.ApplicationPath +
                          String.Format(@"\Plugins\GeekyIrc\Logs\{0}-Chat.log", StyxWoW.Me.Name);


            try
            {
                string dir = Logging.ApplicationPath + @"\Plugins\GeekyIrc\Logs\";
                if (!Directory.Exists(dir))
                    Directory.CreateDirectory(dir);
                StreamWriter sw = File.AppendText(path);
                string logLine = String.Format("{0}: {1}.", DateTime.Now, msg);
                sw.WriteLine(logLine);
                sw.Close();
            }
            catch (Exception file)
            {
                Logging.WriteDebug(String.Format("[CHATLOG] {0}", file.Message));
            }
        }

        /// <summary>
        ///   Give OP to the person who requested it
        /// </summary>
        /// <param name="data"> </param>
        public static void OpMe(IrcMessageData data)
        {
            foreach (var meNick in ConfigValues.Instance.IrcUsername)
            {
                ChannelUser meUsr = Irc.GetChannelUser(ConfigValues.Instance.IrcChannel, meNick);

                if (!meUsr.IsOp)
                    Irc.SendReply(data, IrcColor.Red + "I'm not a channel op, sorry!");
                else
                {
                    if (Irc.GetChannelUser(data.Channel, data.Nick).IsOp)
                        Irc.SendReply(data, IrcColor.Red + "You're already a channel operator!");
                    else
                    {
                        Irc.RfcPrivmsg(data.Nick, "+o on you!");
                        Irc.Op(data.Channel, data.Nick);
                    }
                }
            }
        }

        /// <summary>
        ///   Write some stuff and send and IrcMessage, Colors for both the IrcMessage and the Written line in HB
        /// </summary>
        /// <param name="clr"> Log Color </param>
        /// <param name="ircClr"> Irc Color </param>
        /// <param name="message"> Message </param>
        public static void WowChatOut(Color clr, string ircClr, string message)
        {
            if (!ConfigValues.Instance.LogItAll)
                SendIrc(ircClr + message);
            else if (ConfigValues.Instance.LogInHbsLog) Write(clr, message);
            ChatList.Add(message);
            WriteChatLog(message);
        }

        /// <summary>
        ///   Write some stuff, only print in the hb log if this is true;
        /// </summary>
        /// <param name="hblog"> Print in HB Log </param>
        /// <param name="clr"> Color </param>
        /// <param name="ircClr"> IrcColor </param>
        /// <param name="message"> Message </param>
        public static void WowChatOut(Color clr, string ircClr, string message, bool hblog)
        {
            if (!ConfigValues.Instance.LogItAll)
                SendIrc(ircClr + message);
            else if (ConfigValues.Instance.LogInHbsLog && hblog) Write(clr, message);


            ChatList.Add(message);
            WriteChatLog(message);
        }

        /// <summary>
        ///   Write some stuff, only print in the hb log if this is true;
        /// </summary>
        /// <param name="hblog"> Print in HB Log </param>
        /// <param name="ircClr"> IrcColor </param>
        /// <param name="message"> Message </param>
        public static void WowChatOut(string ircClr, string message, bool hblog)
        {
            WowChatOut(Color.Yellow, ircClr, message, hblog);
        }
    }
}